package com.jianyun.jyzs.dao;

import android.content.Context;

import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.stmt.DeleteBuilder;
import com.j256.ormlite.stmt.QueryBuilder;
import com.jianyun.jyzs.bean.EnginnerMsgResult;
import com.jianyun.jyzs.bean.MessageData;
import com.jianyun.jyzs.db.DatabaseHelper;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by yangshijun on 2018/7/5.
 * 工程动态消息列表
 */

public class EnginnerMsgDao {


    private DatabaseHelper helper;
    private Dao dao;

    public EnginnerMsgDao(Context context) {
        try {
            helper = DatabaseHelper.getHelper(context);
            dao = helper.getDao(MessageData.class);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public void insert(MessageData messageData) {
        try {
            dao.createOrUpdate(messageData);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public void delete() {

        try {
            DeleteBuilder<MessageData, Integer> builder = dao.deleteBuilder();
            builder.delete();
        } catch (SQLException e) {


        }
    }

    /**
     * 按时间倒序
     * @param userId
     * @return
     */
    public List<MessageData> getEnginmsgByUserId(String userId) {
        QueryBuilder<MessageData, Integer> builder = dao.queryBuilder().orderBy("dt",false);
        try {
            List<MessageData> list = builder.where().eq("userId", userId).query();
            if (list != null) {
                return list;
            } else {
                return null;
            }

        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }
}
